import store from '@/store';
import axios from '@/utils/axios';

/** @method 获取菜谱列表 */
export async function getMenuList() {
	const res = await axios.get('/showapi_cpType').then((res) => {
		return res.data;
	});

	if (res.showapi_res_body) {
		return res.showapi_res_body;
	}
}
/** @method 获取菜谱分类列表 */
export async function getMenuTypeList() {
	// await axios.get('/showapi_cpType').then(() => {
	// 	isBol = true;
	// });
}
/**
 * @method 获取菜谱详情
 * @param {String} cpName 菜谱名称
 * @param {String} id 菜谱标识,查询具体某个菜谱信息.当传递该参数的时候,type参数将无效
 * @param {String} maxResults 每次请求返回的最大数据集,取值范围1-50之间
 * @param {String} page 分页的当前页数
 * @param {String} type 菜谱的分类
 *  */
export async function getMenuDetail({ cpName, id, maxResults, page, type }) {
	const res = await axios
		.get('/showapi_cpQuery', {
			params: {
				cpName,
				id,
				maxResults,
				page,
				type,
			},
		})
		.then((res) => res.data)
		.catch((err) => err);

	if (res?.showapi_res_body?.ret_code == '0') {
		return res.showapi_res_body;
	} else {
		if (res.response.status == 555) {
			store.commit('loading', false);
			return {
				ret_code: 555,
				ret_msg: '没有找到该菜谱',
			};
		}
	}
}

/**@method 获取今日菜谱 */
export async function getRecommendTodayMenuList() {
	const FoodsList = await getMenuList();
	const obj = [];

	function hanlderMenuKeys(FoodsList) {
		return Object.keys(FoodsList).splice(2).reverse();
	}
	const MenuKeys = hanlderMenuKeys(FoodsList);

	MenuKeys.forEach(async (type) => {
		const { datas } = await getMenuDetail({
			type,
			page: 1,
			maxResults: 5,
		});

		obj.push({
			title: type,
			data: datas,
		});
	});

	return obj;
}
